package com.ziq.zcamp.place

import android.content.Context
import android.widget.LinearLayout
import androidx.core.content.res.ResourcesCompat
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.place.AreaItem
import com.ziq.zcamp.bean.place.FilterAreaCategory
import com.ziq.zcamp.bean.place.FilterAreaData
import com.ziq.zcamp.databinding.ItemFilterAreaContentAreaHeaderNewBinding
import com.ziq.zcamp.databinding.ItemFilterAreaContentAreaNewBinding
import com.ziq.zcamp.databinding.ItemFilterAreaContentCityHeaderNewBinding
import com.ziq.zcamp.databinding.ItemFilterAreaContentCityNewBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.view.layoutmanager.FilterAreaCategoryContentGridLayoutManager


class CampListFilterAreaCategoryContentAdapter(context: Context?) : GroupedRecyclerViewAdapter(context) ,
    FilterAreaCategoryContentGridLayoutManager.ChildSpanSize {

    val TYPE_HEADER_AREA = 1 //网格
    val TYPE_CHILD_AREA = 2
    val TYPE_HEADER_CITY = 3 // 整行
    val TYPE_CHILD_CITY = 4

    var onPlaceSelectResultListener: OnPlaceSelectResultListener? = null


    var dataList: MutableList<MutableMap<FilterAreaCategory, MutableList<AreaItem>>> = mutableListOf()
    var cityStartGroupPosition = 0
    var provincesStartGroupPosition = 0
    var hotCityStartGroupPosition = 0

    var hasProvinces = false
    var hasHotCity = false

    fun setData(filterAreaData: FilterAreaData?, groupList: MutableMap<String, MutableList<AreaItem>>?){
        cityStartGroupPosition = 0
        provincesStartGroupPosition = 0
        hotCityStartGroupPosition = 0
        dataList.clear()
        hasProvinces = false
        hasHotCity = false
        if(filterAreaData?.historyList?.isNotEmpty() == true){
            cityStartGroupPosition++
            provincesStartGroupPosition++
            hotCityStartGroupPosition++
            dataList.add(mutableMapOf(
                Pair(
                    FilterAreaCategory(PlaceAreaViewModel.area_category_history, GlobalContextManager.getString(R.string.history_area)),
                    filterAreaData.historyList!!
                )
            ))
        }
        if(filterAreaData?.provinces?.isNotEmpty() == true){
            cityStartGroupPosition++
            hotCityStartGroupPosition++
            hasProvinces = true
            dataList.add(mutableMapOf(
                Pair(
                    FilterAreaCategory(PlaceAreaViewModel.area_category_provinces, GlobalContextManager.getString(R.string.all_province)),
                    filterAreaData.provinces!!
                )
            ))
        }

        if(filterAreaData?.hot_cities?.isNotEmpty() == true){
            cityStartGroupPosition++
            hasHotCity = true
            dataList.add(mutableMapOf(
                Pair(
                    FilterAreaCategory(PlaceAreaViewModel.area_category_hot_city, GlobalContextManager.getString(R.string.hot_city)),
                    filterAreaData.hot_cities!!
                )
            ))
        }

        if(groupList != null){
            cityStartGroupPosition++
            dataList.add(mutableMapOf(
                Pair(
                    FilterAreaCategory(PlaceAreaViewModel.area_category_city, GlobalContextManager.getString(R.string.place_all_city)),
                    mutableListOf()
                )
            ))
            for (label in PlaceAreaViewModel.GROUP_LABEL){
                if(groupList.containsKey(label)){
                    dataList.add(mutableMapOf(
                        Pair(
                            FilterAreaCategory(PlaceAreaViewModel.area_category_city_item, label),
                            groupList[label]!!
                        )
                    ))
                }
            }
        }

        notifyDataSetChanged()
    }

    private var selectedId : Long? = null
    fun updateSelectedId(selectedId : Long?){
        this.selectedId = selectedId
        notifyDataSetChanged()
    }

    fun getGroupPosition(category : String) : Int{
        val length = dataList.size
        for (i in 0 until length){
            val temp = dataList[i].keys.first().category
            if(category == PlaceAreaViewModel.area_category_city){
                if(temp == PlaceAreaViewModel.area_category_hot_city ||
                    temp == PlaceAreaViewModel.area_category_city ||
                    temp == PlaceAreaViewModel.area_category_city_item ){
                    return countGroupRangeItem(0, i)
                }
            }else if(category == temp){
                return countGroupRangeItem(0, i)
            }
        }
        return 0
    }

    fun getCityGroupPosition(groupPosition:Int) : Int{
        return countGroupRangeItem(0, cityStartGroupPosition + groupPosition)
    }

    fun getFirstVisibleCategory(firstPosition:Int) : FilterAreaCategory?{
        val groupPosition = getGroupPositionForPosition(firstPosition)
        if(groupPosition >= 0){
            return dataList[groupPosition].keys.first()
        }
        return null
    }


    override fun getChildSpanSize(groupPosition: Int, childPosition: Int): Int {
        if(dataList[groupPosition].keys.first().category == PlaceAreaViewModel.area_category_city_item){
            return 3
        }
        return 1
    }

    override fun getGroupCount(): Int {
        return dataList.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return dataList[groupPosition].values.first().size
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return true
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun getHeaderViewType(groupPosition: Int): Int {
        return if(dataList[groupPosition].keys.first().category == PlaceAreaViewModel.area_category_city_item){
            TYPE_HEADER_CITY
        } else {
            TYPE_HEADER_AREA
        }
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return if(dataList[groupPosition].keys.first().category == PlaceAreaViewModel.area_category_city_item){
            TYPE_CHILD_CITY
        } else {
            TYPE_CHILD_AREA
        }
    }

    override fun getHeaderLayout(viewType: Int): Int {
        return if(viewType == TYPE_HEADER_CITY){
            R.layout.item_filter_area_content_city_header_new
        } else {
            R.layout.item_filter_area_content_area_header_new
        }
    }

    override fun getChildLayout(viewType: Int): Int {
        return if(viewType == TYPE_CHILD_CITY){
            R.layout.item_filter_area_content_city_new
        } else {
            R.layout.item_filter_area_content_area_new
        }
    }

    override fun getFooterLayout(viewType: Int): Int {
        return 0
    }

    override fun onBindHeaderViewHolder(holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?, groupPosition: Int) {
        holder?.let {
            val item = dataList.get(groupPosition).keys.first()
            val category = dataList.get(groupPosition).keys.first().category
            if(category == PlaceAreaViewModel.area_category_city_item){
                val binding = ItemFilterAreaContentCityHeaderNewBinding.bind(holder.itemView)
                binding.title.text = item.name
            } else {
                val binding = ItemFilterAreaContentAreaHeaderNewBinding.bind(holder.itemView)
                binding.title.text = item.name
            }
        }
    }


    override fun onBindChildViewHolder(
        holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        holder?.let {
            val item = dataList.get(groupPosition).values.first()[childPosition]
            val itemCount = dataList.get(groupPosition).values.first().size
            val category = dataList.get(groupPosition).keys.first().category
            if(category == PlaceAreaViewModel.area_category_city_item){
                val binding = ItemFilterAreaContentCityNewBinding.bind(holder.itemView)
                binding.name.text = item.name
                val lp = (binding.name.layoutParams as LinearLayout.LayoutParams)
                if(childPosition == itemCount - 1){
                    lp.bottomMargin = 28.dp
                }else {
                    lp.bottomMargin = 24.dp
                }
                binding.name.layoutParams = lp

                holder.itemView.click {
                    onPlaceSelectResultListener?.onPlaceSelect(item, "filter")
                }
            } else {
                val binding = ItemFilterAreaContentAreaNewBinding.bind(holder.itemView)
                binding.name.text = item.name
                binding.name.isSelected = selectedId == item.id
                holder.itemView.click {
                    onPlaceSelectResultListener?.onPlaceSelect(item, "filter")
                }
            }
        }
    }

    override fun onBindFooterViewHolder(holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?, groupPosition: Int) {
    }

}